package service;

import dao.ClassicFormulaCompositionDao;
import dao.ClassicFormulaDao;
import dao.TcmDao;
import dto.CFQueryListDto;
import pojo.ClassicFormula;
import pojo.ClassicFormulaComposition;
import pojo.Tcm;
import util.PageUtil;

import java.util.List;

public class ClassicFormulaService {
    ClassicFormulaDao cfDao=new ClassicFormulaDao();
    ClassicFormulaCompositionDao cFCDao=new ClassicFormulaCompositionDao();
    TcmDao tcmDao=new TcmDao();
//展示
    public PageUtil getClassicFormulaList(int curPage){
        //        设置一页的记录数
        int pageSize=6;
//        获取总记录数
        int totalNum = cfDao.getTotalNum();
        PageUtil<ClassicFormula> pu = new PageUtil<>(curPage, pageSize, totalNum);

        //查当前页数据
        List<ClassicFormula> list = cfDao.getClassicFormulaList(pu.getCurrentPage(),pu.getPageSize());


        //将查询到的指定页码的数据存储到分页工具对象中
        pu.setList(list);
        //将分页工具对象返回
        return pu;
    }

//查询功能
    public PageUtil getQueryList(String key,int flag){
        List<ClassicFormula> list =cfDao.getQueryCFList(key,flag);
        int pageSize=6;
        int totalNum=list.size();
        PageUtil<ClassicFormula> pu = new PageUtil<>(1, pageSize, totalNum);
        pu.setList(list);
        return pu;
    }


//    细节展示
    public CFQueryListDto getDetail(int id){
        CFQueryListDto dto=new CFQueryListDto();
        dto.setCf(cfDao.getClassicFormulaById(id));
        dto.setCfcList(cFCDao.getCFCListByCFId(id));
        for(int i=0;i<dto.getCfcList().size();i++){
            dto.getCfcList().get(i).setTcm(tcmDao.getDetail(dto.getCfcList().get(i).getTcmid()));
        }

        return dto;
    }
}
